package net.thermetics.fx.features;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;

public class ScopedComponent implements Component {
	private Feature parentFeature;
	private String id;
	private String codeUrl;
	
	@Override
	public String getId() {
		return id;
	}

	@Override
	public void setId(String id) {
		this.id = id;	
	}

	@Override
	public String getCodeUrl() {
		if (codeUrl != null) {
			return codeUrl;
		}
		else {
			return "http://thermetics-fx.appspot.com/features/" + parentFeature.getId() + "/" + getId() + ".data";
		}
	}

	@Override
	public void setCodeUrl(String url) {
		codeUrl = url;		
	}

	@Override
	public String getContentType() {
		return "text/javascript";
	}

	@Override
	public String render() {
        String pkg = this.getClass().getPackage().getName();
        String filename = pkg + ".main";
        filename = filename.replace(".", "/");
        filename += ".js";
        return filename;
	}

	@Override
	public void verify() {
		if (!parentFeature.getComponents().contains(this)) {
			throw new IllegalStateException("Component \"" + this + "\" invalid: Parent feature does not contain this component.");
		}
	}
	
	@Override
	public String toString() {
		return getId();
	}

}
